package u1;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * Reads from the "enwiki-...-pages-articles.xml" format (actually anything with
 * a "title" tag followed by a "text" tag will do). Extracts article titles and
 * text. This hides most of the unpleasant details of stream parsing.
 * 
 * It is the caller's responsibility to close the input stream, if the stream
 * needs to be closed.
 */
public abstract class PageReader {

  private final XMLReader xr;
  private int pageIndex;

  /**
   * @throws SAXException 
   */
  public PageReader() throws SAXException {
    super();
    xr = XMLReaderFactory.createXMLReader();
    xr.setContentHandler(new DefaultHandler() {
      private String titleString = "";
      private boolean title = false;
      private boolean text = false;
      private StringBuilder buf = new StringBuilder();

      @Override
      public void characters(char[] ch, int start, int length)
          throws SAXException {
        // We don't get all the chars at once; it delivers them in blocks so
        // we have to buffer them.
        buf.append(ch, start, length);
      }

      @Override
      public void endElement(String uri, String localName, String name)
          throws SAXException {
        if (title) {
          titleString = buf.toString();
          title = false;
        } else if (text) {
          readPage(titleString, buf.toString());
          ++pageIndex;          
          text = false;
        }
      }

      @Override
      public void startElement(String uri, String localName, String name,
          Attributes attributes) throws SAXException {
        if ("title".equals(localName)) {
          buf.delete(0, buf.length());
          title = true;
        }
        else if ("text".equals(localName)) {
          buf.delete(0, buf.length());
          text = true;
        }
      }
    });
  }
  
  /**
   * Call this to parse input with the reader.
   * 
   * @param is not null; this method does not close the stream
   * 
   * @throws SAXException 
   * @throws IOException 
   */
  public void parse(InputStream is) throws IOException, SAXException {
    xr.parse(new InputSource(is));
  }
  
  /**
   * Index (starting at zero) of the page for the current call to readPage;
   * also, the total number of pages read, after parse returns.
   * 
   * @return non-negative
   */
  public int getPageIndex() {
    return pageIndex;
  }

  /**
   * This is called by parse when a page is read in. You must override this
   * method in a subclass.
   * 
   * @param title not null
   * @param text not null
   */
  protected abstract void readPage(String title, String text);
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

